# -*- encoding=utf8 -*-
import os
from support.device.app.manager import *
from support.system.os import *

def testingMethod(item, attrs):
    if(attrs['touchable']):
        item.click()
    if(attrs['editable']):
        print(attrs['text'])
        
if __name__ == "__main__":
    shortArgsMap = {"p:":"需要遍历的APK PackageName", "s:":"需要连接设备的序列号", "a:":"需要遍历的Activity", "d:":"遍历深度"}
    longArgsMap = {"path=":"数据保存目录", "debug=":"功能调试(1-显示已保存的图像 2-匹配算法 3-重匹配)"}
    userInputMap = {"pkg":"-p", "serial":"-s", "datapath":"--path", "activity":"-a", "debug":"--debug", "depth":"-d"}
    args = Opts(shortArgsMap, longArgsMap)
    args.getUserInput(userInputMap)
    if(userInputMap["datapath"] is None):
        userInputMap["datapath"] = os.path.dirname(__file__)
    if(userInputMap["depth"]):
        userInputMap["depth"] = int(userInputMap["depth"])
    else:
        userInputMap["depth"] = 5
    if(userInputMap["debug"] == "1"):
        AppManager.showBlob(userInputMap["datapath"], userInputMap["pkg"], userInputMap["activity"])
    else:
        am = AppManager(userInputMap["serial"], userInputMap["pkg"], userInputMap["activity"], userInputMap["datapath"], userInputMap["depth"])
        am.stop()
        am.start()
        if(userInputMap["debug"] == "2"):
            am.uiCrawOnlyFromDB(userInputMap["activity"])
        elif(userInputMap["debug"] == "3"):
            am.uiCrawOnlyCurrent(userInputMap["activity"])
        else:
            am.uiCrawl(testingMethod)
        am.stop()
        am.clear()